/**
 * Date:2017年10月17日下午3:15:19
 *
 */

package com.app.action;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.dao.Product;
import com.app.dao.ProductKey;
import com.app.dao.ProductRepository;

import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 
 * Description: 系统控制器,主要处理一些不需要登录就能操作的逻辑 <br/>
 * Date: 2018年10月11日 下午4:22:09 <br/>
 *
 * @author xuejianxinokok@163.com
 */
@Controller
@RequestMapping
public class AppController {
	private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AppController.class);

	@Autowired
	ProductRepository productRepository;

	@GetMapping(value = "/get")
	@ResponseBody
	@Tag(name = "查询产品")
	public Product get(@RequestParam Integer id, @RequestParam String storeId) throws UnknownHostException {
	    System.out.println("111");
	    
		return productRepository.findById(new ProductKey(id, storeId)).orElseGet(Product::new);		
	}

	@GetMapping(value = "/hello")
	@ResponseBody
	@Tag(name = "插入产品")
	public String hello() throws UnknownHostException {
		Product pa = new Product();
		pa.setStoreId("a");
		pa.setTitle("titleA");
		productRepository.save(pa);

		Product pb = new Product();
		pb.setStoreId("b");
		pb.setTitle("titleB");
		productRepository.save(pb);

		InetAddress ia = InetAddress.getLocalHost();
		String host = ia.getHostName();// 获取计算机主机名
		String ip = ia.getHostAddress();// 获取计算机IP
		String msg = System.currentTimeMillis() + " test ok";
		log.info(msg);
		return String.format("host=%s\nip=%s\nmsg=%s\n", host, ip, msg);
	}

}